package com.ikun.service.impl;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ikun.dao.CleaningOrdersDao;
import com.ikun.entity.CleaningOrders;
import com.ikun.entity.req.CleaningOrderDTO;
import com.ikun.service.CleaningOrdersService;
import com.ikun.util.Result;

@Service
public class CleaningOrdersServiceImpl implements CleaningOrdersService {

    @Autowired
    private CleaningOrdersDao cleaningOrdersDao;

    @Override
    public List<CleaningOrders> findAssignedOrders() {
        return cleaningOrdersDao.findAssignedOrders();
    }

    @Override
    public List<CleaningOrders> findCompleted() {
        return cleaningOrdersDao.findCompleted();
    }

    @Override
    public List<CleaningOrders> findCreate() {
        return List.of();
    }

    @Override
    @Transactional
    public Result getPendingOrders(Integer staffId) {
        List<CleaningOrders> orders = cleaningOrdersDao.getPendingOrders(staffId);
        int totalCount = cleaningOrdersDao.getPendingOrdersCount(staffId);

        Map<String, Object> data = new HashMap<>();
        data.put("totalCount", totalCount);
        // 2025-04-02 19:08:00吴思伟修改 orders 为 cleanOrders
        data.put("orders", orders);
        return Result.success(data);

    }

    @Override
    public String createCleaning(CleaningOrders cleaningOrders) {
        int i = cleaningOrdersDao.createCleaning(cleaningOrders);
        if (i > 0) {
            return "保洁预约成功";
        }
        return "保洁预约失败";
    }

    @Override
    public List<CleaningOrderDTO> getAllWorkOrdersByMangerId(Integer mangerId) {
        return cleaningOrdersDao.getAllWorkOrdersByMangerId(mangerId);
    }

    @Override
    public int assignStaff(Integer orderId, Integer staffId) {
        return cleaningOrdersDao.assignStaff(orderId, staffId);
    }

    @Override
    public Result markOrderCompleted(String orderId, String staffId) {
        int i = cleaningOrdersDao.markOrderCompleted(orderId, staffId);
        if (i > 0) {
            Map<String, Object> data = new HashMap<>();
            data.put("complete_time", LocalDateTime.now().toString());
            data.put("staffId", 1);
            data.put("quality_check", "待检查");
            return Result.success("保洁工单状态更新成功", data);
        }
        return Result.fail("保洁工单状态更新失败");
    }

}
